package com.example.smartcity_ikun3.ui.home.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_ikun3.R
import com.example.smartcity_ikun3.logic.util.Util.show
import com.example.smartcity_ikun3.ui.App
import com.example.smartcity_ikun3.ui.home.HomeViewModel
import com.example.smartcity_ikun3.ui.home.activity.NewsActivity
import com.example.smartcity_ikun3.ui.home.adapter.ReAdapterNews
import com.example.smartcity_ikun3.ui.home.model.NewsEntity
import kotlinx.android.synthetic.main.activity_news.view.*
import kotlinx.android.synthetic.main.fragment_news_show.*


class NewsShowFragment : Fragment() {

    private val viewModel by lazy {
        ViewModelProviders.of(this).get(HomeViewModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_news_show, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val array = ArrayList<NewsEntity>()
        NewsRecyclerView.apply {
            this.layoutManager = LinearLayoutManager(context)
            this.adapter = ReAdapterNews(array)
        }
        viewModel.setNews(idNum)
        viewModel.newsList.clear()
        viewModel.newsLiveData.observe(this, Observer {
            if (it.code == 200){
                viewModel.newsList.clear()
                viewModel.newsList.addAll(it.rows!!)
            }

            for (i in viewModel.newsList){
                array.add(NewsEntity(i.title.toString(), App.url + i.cover, i.content.toString(), i.publishDate.toString()))
                NewsRecyclerView.adapter?.notifyItemChanged(array.size)
            }
        })
    }

    companion object{
        var idNum = 0
    }
}